package arr;

import java.util.Arrays;

/**
 * @author : Rookie
 * @description : 移除指定的元素
 * @date : 2023/8/19
 */
public class DeleteElement {

    public static void main(String[] args) {
        int target = 2;
        int[] nums = new int[] {0,1,2,2,2,3,0,4,2};
        System.out.println(doublePoint(target,nums));
    }
    
    private static int violentSolute(int target,int[] nums) {
        int result = nums.length;
        for (int i = 0; i < result; i++) {
            if (nums[i] == target) {
                // 发现数组元素等于目标值，就将当前元素之后的元素往前移动1位
                for (int j = i+1; j < result; j++) {
                    nums[j-1] = nums[j];
                }
                i--;
                result--;
            }
            print(i,nums);
        }
        return result;
    }

    private static int doublePoint(int target,int[] nums) {
        int slow = 0;
        for (int fast = 0; fast < nums.length; fast++) {
            if (nums[fast] != target) {
                nums[slow] = nums[fast];
                slow++;
            }
        }
        return slow;
    }

    private static void print(int round,int[] arr) {
        System.out.println("第" + round +"轮" + "数组元素：" + Arrays.toString(arr));
    }
}
